package com.atguigu.gulimail.ware.listener;

import com.atguigu.common.to.OrderTo;
import com.atguigu.common.to.wareTo.StockLockedTo;
import com.atguigu.gulimail.ware.service.WareSkuService;
import com.atguigu.gulimail.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
@RabbitListener(queues = {"stock.release.stock.queue"})
public class StockReleaseListener {

    @Autowired
    WareSkuService wareSkuService;

    @RabbitHandler
    public void handlerStockLock(Message message, Channel channel, StockLockedTo to) throws IOException {

        try {
            System.out.println("时间到，库存解锁");
            wareSkuService.unlockStockM(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @RabbitHandler
    public void handlerOrderClose(Message message, Channel channel, OrderTo vo) throws IOException {
        try{
            System.out.println("监听到OrderClose消息，从这里解锁库存");
            wareSkuService.unlockStockM(vo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }


    }

}
